package ycl.scheduled.common.config;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import ycl.scheduled.common.constants.HtmlConstant;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * swagger配置
 *
 * @author: YCL
 * @date: 2021-06-06 15:00:00
 */
@Configuration
//@EnableKnife4j
@EnableSwagger2
public class SwaggerConfig {

	@Value("${project.controller}")
	private String packageController;

	@Bean(value = "sys")
	public Docket sys() {
		return getDocket("sys", "用户接口");
	}

	@Bean(value = "region")
	public Docket region() {
		return getDocket("region", "地区接口");
	}

	@Bean(value = "hovel")
	public Docket hovel() {
		return getDocket("hovel", "杂物间接口");
	}

	@Bean(value = "security")
	public Docket security() {
		return getDocket("security", "权限接口");
	}

	/**
	 * 获取docket
	 *
	 * @return docket
	 */
	private Docket getDocket(String mapping, String groupName) {
		String basePackage = packageController;
		Predicate<String> paths;
		if (StrUtil.isNotBlank(mapping)) {
			basePackage = basePackage + "." + mapping;
			paths = PathSelectors.ant("/" + mapping + "/**");
		} else {
			paths = PathSelectors.any();
		}
		return new Docket(DocumentationType.SWAGGER_2)
				.useDefaultResponseMessages(false)
				.apiInfo(apiInfo())
				.groupName(groupName)
				.select()
				.apis(RequestHandlerSelectors.basePackage(basePackage))
				.paths(paths)
				.build()
				.securitySchemes(securitySchemes())
				.securityContexts(securityContexts());
	}

	private List<SecurityContext> securityContexts() {
		ArrayList<SecurityContext> list = Lists.newArrayList();
		SecurityContext build = SecurityContext.builder()
				.securityReferences(securityReferences())
				.build();
		list.add(build);
		return list;
	}

	/**
	 * 全局认证
	 *
	 * @return 认证列表
	 */
	private List<SecurityReference> securityReferences() {
		AuthorizationScope scope = new AuthorizationScope("global", "accessEverything");
		AuthorizationScope[] scopes = new AuthorizationScope[1];
		scopes[0] = scope;
		ArrayList<SecurityReference> list = Lists.newArrayList();
		list.add(new SecurityReference(HtmlConstant.TOKEN, scopes));
		return list;
	}

	/**
	 * 默认请求头token
	 *
	 * @return 认证列表
	 */
	private List<SecurityScheme> securitySchemes() {
		ArrayList<SecurityScheme> list = Lists.newArrayList();
		list.add(new ApiKey(HtmlConstant.TOKEN, HtmlConstant.TOKEN, "header"));
		return list;
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("swagger测试")
				.description("swagger-bootstrap-ui")
				.termsOfServiceUrl("http://localhost:8079/")
				.version("1.0")
				.build();
	}

}

